﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDDshop2.Controllers
{
    public class PlaceController : Controller
    {
        //
        // GET: /Place/

        //显示购物车中的数据
        public ActionResult Place()
        {
            if (Session["car"] != null)
            {
                //获取session中的数据
                List<Car> carlist = Session["car"] as List<Car>;
                //判断session是否为空
                if (carlist == null)
                {
                    //为空的话需要实例化
                    carlist = new List<Car>();
                }
                //讲数据存至viewbag中 等待视图调用
                ViewBag.list = carlist;
                decimal money = 0;
                foreach (Car c in carlist)
                {
                    money += c.price;
                }
                ViewBag.money = money;
                //List<type> ty= bll.Class1.select_type();
                //ViewBag.aaa = ty;
                return View();
            }
            else {
                Response.Write("<script>alert('购物车是空的！');</Script>");
                return RedirectToAction("Cart","Cart");
            }
        }

        //结算添加订单
        public ActionResult money() 
        {
            List<Car> carlist = Session["car"] as List<Car>;
            //判断session是否为空
            if (carlist == null)
            {
                //为空的话需要实例化
                carlist = new List<Car>();
                return RedirectToAction("Car");
            }
            else
            {
                //创建订单
                int userid = Convert.ToInt32(Session["userid"]);
                string orderid = Convert.ToString(System.Guid.NewGuid());
                string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                bool i = BLL.Bll.insert_order(userid, orderid, dt);
                if (i)
                {
                    foreach (Car c in carlist)
                    {
                        //逐条语句创建详情订单
                        decimal price = c.price;
                        int bookid = c.ID;
                        BLL.Bll.insert_order_det(orderid, price, bookid);
                    }
                }
            }
            Session["car"] = null;
            return RedirectToAction("Index","Home");
        }
    }
}
